//
//  GQHQutationsController.m
//  Xanthium
//
//  Created by GuanQinghao on 09/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHQutationModel.h"

#pragma mark Object

#pragma mark View
#import "GQHQutationsView.h"

#pragma mark Controller
#import "GQHQutationsController.h"
#import "GQHContractDetailController.h"
#import "GQHFreightInfoController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHQutationsController () <GQHQutationsViewDelegate> {
    
#pragma mark Data
    /// 请求参数
    NSMutableDictionary *parameters;
    /// 返回模型数组
    __block NSMutableArray<GQHQutationModel *> *qutationModelArray;
#pragma mark View
    /// 自定义根视图
    GQHQutationsView *rootView;
}

@end


@implementation GQHQutationsController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHQutationsView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"userId"];
    
    [self fetchServiceDataWithParameters:parameters];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"我的报价";
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [rootView.qh_tableView.mj_header beginRefreshing];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [rootView.qh_tableView.mj_header endRefreshing];
}

#pragma mark --Delegate
- (void)qh_dialMobile:(UIButton *)sender {
    DLog(@"联系货主");
    
    GQHQutationModel *qutationModel = qutationModelArray[sender.tag];
    [self qh_dialWithMobile:qutationModel.qh_loaderMobile];
}

- (void)qh_acceptContractOrNot:(UIButton *)sender {
    DLog(@"确认承运协议或取消报价");
    
    GQHQutationModel *qutationModel = qutationModelArray[sender.tag];
    if ([qutationModel.qh_quoteState isEqualToString:@"01"]) {
        
        /// 已报价 01
        
        ///重新报价
        if (qutationModel.qh_freightWeight == 0)
        {
            [self getResourceInfo:qutationModel.qh_freightID];
        }
        else
        {
            
        }
    }
    if ([qutationModel.qh_quoteState isEqualToString:@"02"]) {
        
        /// 已中标 02
        
        ///查看协议
//        [self checkContractWithID:qutationModel.qh_contractID];
        [self acceptContractWithID:qutationModel.qh_contractID];
    }
    if ([qutationModel.qh_quoteState isEqualToString:@"03"]) {
        
        /// 已承运 03
        [self checkContractWithID:qutationModel.qh_contractID];
    }
}

- (void)getResourceInfo:(NSString *)sourcesId
{
    NSString * url = [[NSString qh_APIURLString] stringByAppendingString:@"/buSources/1/findOne"];
    [GQHNetHelper qh_requestGET:url parameters:@{@"token":USER_GET_ID, @"id":sourcesId} success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        GQHFreightModel *freightModel = [GQHFreightModel yy_modelWithDictionary:data[@"list"][0]];
        GQHFreightInfoController * freight = [GQHFreightInfoController new];
        freight.qh_freightModel = freightModel;
        [self.navigationController pushViewController:freight animated:YES];
        
    } failure:^(NSError *error) {
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchServiceDataWithParameters:(NSMutableDictionary *)parameters {
    
    __block int page = 1;
    
    [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
    [parameters setObject:ROWS forKey:@"rows"];
    
    rootView.qh_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page = 1;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHQutationModel qh_fetchQutationsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHQutationModel *> *qutations) {
            
            if (status) {
                
                qutationModelArray = [NSMutableArray arrayWithArray:qutations];
                rootView.qh_qutationModelArray = qutationModelArray;
                [rootView.qh_tableView reloadData];
            } else {
                
                [self showToastWithText:message];
            }
            [rootView.qh_tableView.mj_header endRefreshing];
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_header endRefreshing];
        }];
    }];
    
    rootView.qh_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        page++;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHQutationModel qh_fetchQutationsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHQutationModel *> *qutations) {
            
            if (status) {
                
                if (qutations && qutations.count > 0) {
                    
                    [qutationModelArray addObjectsFromArray:qutations];
                    rootView.qh_qutationModelArray = qutationModelArray;
                    [rootView.qh_tableView reloadData];
                    [rootView.qh_tableView.mj_footer endRefreshing];
                } else {
                    
                    [rootView.qh_tableView.mj_footer endRefreshingWithNoMoreData];
                }
            } else {
                
                [self showToastWithText:message];
                [rootView.qh_tableView.mj_footer endRefreshing];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_footer endRefreshing];
        }];
    }];
}

- (void)cancelQutation:(UIButton *)sender {
    DLog(@"司机取消报价");
    
    GQHQutationModel *qutationModel = qutationModelArray[sender.tag];
    NSMutableDictionary *para = [NSMutableDictionary dictionary];
    [para setObject:qutationModel.qh_qutationID forKey:@"id"];
    [para setObject:qutationModel.qh_freightID forKey:@"sourcesId"];
    
    NSString *APIURLString = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"/buQuotationRecord/1/cancel"];
    
    [GQHNetHelper qh_requestGET:APIURLString parameters:para success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@---%@---%@",code,message,data);
        
        if (0 == [code integerValue]) {
            
            [rootView.qh_tableView.mj_header beginRefreshing];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

- (void)qh_canclePrice:(UIButton *)sender
{
    [self cancelQutation:sender];
}

- (void)acceptContractWithID:(NSString *)contractID {
    DLog(@"确认承运");
    
    GQHContractDetailController *contractDetailController = [[GQHContractDetailController alloc] init];
    contractDetailController.qh_contractID = contractID;
    contractDetailController.qh_acceptable = NO;
    contractDetailController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:contractDetailController animated:YES];
}

- (void)checkContractWithID:(NSString *)contractID {
    DLog(@"查看协议");
    
    GQHContractDetailController *contractDetailController = [[GQHContractDetailController alloc] init];
    contractDetailController.qh_contractID = contractID;
    contractDetailController.qh_acceptable = YES;
    contractDetailController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:contractDetailController animated:YES];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
